package com.lineage.data.npc.mob;

import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Party;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_PacketBoxGree;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.CheckUtil;
import java.io.IOException;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MobTeleport extends NpcExecutor {
  private static final Log _log = LogFactory.getLog(MobTeleport.class);
  
  private int _locx = 0;
  
  private int _locy = 0;
  
  private int _mapid = 0;
  
  private boolean _party = false;
  
  public static NpcExecutor get() {
    return new MobTeleport();
  }
  
  public int type() {
    return 8;
  }
  
  public L1PcInstance death(L1Character lastAttacker, L1NpcInstance npc) {
    try {
      L1PcInstance pc = CheckUtil.checkAtkPc(lastAttacker);
      if (pc != null && this._locx != 0 && this._locy != 0) {
        M_teleport teleport = new M_teleport(pc);
        teleport.stsrt_cmd();
      } 
      return pc;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return null;
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._locx = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      this._locy = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
    try {
      this._mapid = Integer.parseInt(set[3]);
    } catch (Exception exception) {}
    try {
      this._party = Boolean.parseBoolean(set[4]);
    } catch (Exception exception) {}
  }
  
  private class M_teleport implements Runnable {
    private final L1PcInstance _pc;
    
    private M_teleport(L1PcInstance pc) {
      this._pc = pc;
    }
    
    private void stsrt_cmd() throws IOException {
      GeneralThreadPool.get().execute(this);
    }
    
    public void run() {
      try {
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree(1));
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree("$ 5秒后将会被传送!"));
        Thread.sleep(1000L);
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree("$ 4秒后将会被传送!"));
        Thread.sleep(1000L);
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree("$ 3秒后将会被传送!"));
        Thread.sleep(1000L);
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree("$ 2秒后将会被传送!"));
        Thread.sleep(1000L);
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree("$ 1秒后将会被传送!"));
        Thread.sleep(1000L);
        this._pc.sendPackets((ServerBasePacket)new S_PacketBoxGree("$ "));
        L1Teleport.teleport(this._pc, MobTeleport.this._locx, MobTeleport.this._locy, 
            (short)MobTeleport.this._mapid, 5, true);
        if (MobTeleport.this._party) {
          L1Party party = this._pc.getParty();
          if (party != null) {
            Iterator<L1PcInstance> iterator = party.partyUsers().values().iterator();
            while (iterator.hasNext()) {
              L1PcInstance otherPc = iterator.next();
              if (otherPc.getId() != party.getLeaderID() && this._pc.getMapId() == otherPc.getMapId())
                L1Teleport.teleport(otherPc, MobTeleport.this._locx, MobTeleport.this._locy, 
                    (short)MobTeleport.this._mapid, 5, true); 
            } 
          } 
        } 
      } catch (Exception e) {
        MobTeleport._log.error(e.getLocalizedMessage(), e);
      } 
    }
  }
}
